import os, sys
import paramiko

hostname = '192.168.0.13'
port = 22
username = 'root'
password = '123.com'
local_path = os.path.join(os.getcwd(),'id_rsa')
remote_path = '/opt/id_rsa'
try:
    #密码方式
    # s = paramiko.Transport((hostname, port))
    # s.connect(username = username, password=password)
    #密钥方式
    s = paramiko.Transport((hostname, port))
    key_file = os.path.join(os.getcwd(),'id_rsa')
    key = paramiko.RSAKey.from_private_key_file(key_file)
    s.connect(username=username, pkey=key)

except Exception as e:
    print(e)

sftp = paramiko.SFTPClient.from_transport(s)
# 使用put()方法把本地文件上传到远程服务器
sftp.put(local_path, remote_path)

